from spiders import EESpider
from configuration import LOG_FILE
import os


def read_log():
    """
    Read log file to determine which page to start
    If the log file does not exist, it will create a new one with start_page = 1
    :return: the start page number
    """
    if os.path.isfile(LOG_FILE) is False:
        with open(LOG_FILE, 'w') as writer:
            writer.write('1')
        return 1

    with open(LOG_FILE, 'r') as reader:
        try:
            start_page = int(reader.readline())
        except ValueError as ve:
            print 'Missing log file!!!'
            exit(-1)
        return start_page


if __name__ == '__main__':

    ee_spider = EESpider(read_log())
    try:
        ee_spider.run()
    except Exception as e:
        print e
    finally:
        ee_spider.browser.quit()
        # ee_spider.display.stop()
        if ee_spider.start_page < ee_spider.current_page:
            with open(LOG_FILE, 'w') as writer:
                writer.write(str(ee_spider.current_page))
                print 'Broken at Page %d!' % (ee_spider.current_page,)